#include <SGF.h>
#include <boost/bind.hpp>
#include <functional>

using namespace SGF;

class TestClass:public IEntity
{
public:
	int hp;
	float speed;
	TestClass()
	{
		printf("Test class created\n");
	}

	virtual void init(MapState* map)
	{
		printf("Ho ho it's me");
		setNode(map->getSceneManager()->addCubeSceneNode(10.0f,map->getMapRootNode()));
		node->setName("hohoohohoho");
		node->setMaterialFlag(irr::video::EMF_LIGHTING,false);
		map->setActive(this,true);
		hp=100;
		speed=0.1f;
	}

	void onMouse(const irr::SEvent::SMouseInput& ev)
	{
		if(ev.Event==irr::EMIE_LMOUSE_PRESSED_DOWN)
		{
			printf("You pressed your left mouse");
		}
	}

	virtual void onAdd()
	{
		map->mouseSignal.connect(boost::bind(&TestClass::onMouse,this,_1),*this);
	}

	virtual void onRemove()
	{
		delete this;
	}

	virtual void update()
	{
		node->setRotation(node->getRotation()+Ogre::Vector3(0,speed,0));
	}

	virtual void onSave(SaveData& saveData) const
	{
		IEntity::onSave(saveData);
		saveData["hp"]=hp;
		saveData["speed"]=speed;
	}

	virtual void onLoad(const SaveData& saveData)
	{
		IEntity::onLoad(saveData);
		hp=saveData["hp"];
		speed=saveData["speed"];
	}

};

PluggableProduct<TestClass> abc("TestClass");



class TestApp: public Game
{
public:
	void initialize(GameInitializationParameters& params)
	{
		params.irrlichtParameters.DriverType=irr::video::EDT_DIRECT3D9;//overide default driver type
		mapManager=new MapManager<>(this);
		Game::initialize(params);
		/*MapState* currentMap=mapManager->newMap();
		currentState=currentMap;
		IEntity* newEnt=(IEntity*)(PluggableFactory::getInstance().create("TestClass"));
		newEnt->init(currentMap);
		currentMap->addEntity(newEnt);
		currentMap->save("test.map");*/
		MapState* currentMap=mapManager->getMap("test.map");
		currentState=currentMap;
		scene::ICameraSceneNode* camera=currentMap->getSceneManager()->addCameraSceneNodeFPS();
		camera->setPosition(core::vector3df(0,0,-20.f));
		camera->setDebugDataVisible(scene::EDS_FULL);
	}

	virtual bool OnEvent(const irr::SEvent& ev)
	{
		if((ev.EventType==irr::EET_KEY_INPUT_EVENT)&&(ev.KeyInput.Key==irr::KEY_F1))
				switchToEditMode();
		return Game::OnEvent(ev);
	}

	void finalize()
	{
		Game::finalize();
	}
}app;